<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->

</head>
<body>
<div style="margin-top: 20px;">
</div>

<form class="layui-form" id="standard">
    <div class="layui-form-item">
        <label class="layui-form-label">选择标品</label>
        <div class="layui-input-inline" style="width: 700px">
            <select id="standard_slt" lay-filter="addStandard" lay-search>
                <option value="">请选择</option>
                {notempty name="$standardData"}
                {volist name="standardData" id="vo"}
                <option value="{$vo.id}">{$vo.name}</option>
                {/volist}
                {/notempty}
            </select>
        </div>
        <span id="addStandard" class="layui-btn layui-btn-danger layui-btn-sm" style="margin-top: 4px">添加</span>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">已选标品</label>
        <div class="layui-input-inline" style="width: 700px;min-height:40px;background: #eee" id="stand_names">
            {notempty name="$standardsArr"}
            {volist name="standardsArr" id="vo"}
            <span class="layui-btn layui-btn-sm layui-btn-normal del" data-id="{$vo['id']}" style="margin: 5px 10px">{$vo['name']}<i class="layui-icon"></i></span>
            {/volist}
            {/notempty}
        </div>
    </div>

    <input name="standIdStr" id="standIdStr" value='{notempty name="$info.ids_str"}{$info.ids_str}{/notempty}' type="hidden">

    <div class="layui-form-item">
        <label class="layui-form-label">折扣设置</label>
        <div class="layui-input-inline" style="width: 200px">
            <input id="discount_rate" name="discount_rate" lay-verify="required" {notempty name="$info.discount_rate"}value="{$info.discount_rate}"{/notempty} placeholder="请输入折扣" autocomplete="off" class="layui-input" type="number" max="1" min="0.1" step="0.05">
        </div>
    </div>

    {notempty name="$info"}
    <input type="hidden" name="id" value="{$info.id}">
    {/notempty}


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="standard">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>





<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>

<script>
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(standard)', function() {
                var discount_rate = $("#discount_rate").val();
                var standIdStr = $("#standIdStr").val();
                if(discount_rate<0.1 || discount_rate>1){
                    layer.msg("请填写正确的折扣值");
                    return false;
                }
                if(standIdStr=='' || standIdStr==undefined){
                    layer.msg("请选择标品");
                    return false;
                }

                $.ajax({
                    url:"{:url('admin/tob/add_discount')}",
                    data:$('#standard').serialize(),
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 1) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                });
                return false;
            });

            //标品选择
            form.on('select(standard)', function(data){
                var standardid=data.value;
                if(standardid!==''){
                    $("#standard").attr("data-id",standardid);
                }
            });

            $("#addStandard").on("click",function () {
                var standardid = $("#standard_slt").val();
                var standardname = $("#standard_slt").next().find('input').val();
                addStandard(standardid,standardname);
            });

            function addStandard(standardid,standardname){
                var standIdStr=$("#standIdStr").val();
                if(standIdStr!=''){
                    var ids=standIdStr.split(",");
                    if($.inArray(standardid,ids)==-1){
                        ids.push(standardid);
                        standIdStr = ids.join(",");
                    }else{
                        layer.msg("请勿重复添加标品");
                        return false;
                    }
                }else{
                    standIdStr = standardid;
                }
                var standspan='<span class="layui-btn layui-btn-sm layui-btn-normal del" data-id="'+standardid+'" style="margin: 5px 10px">'+standardname+'<i class="layui-icon"></i></span>';
                $("#stand_names").append(standspan);
                $("#standIdStr").val(standIdStr);
            }

            $(document).on("click",'.del',function () {
                var dataid=$(this).attr("data-id");
                var standids=$("#standIdStr").val();
                var ids=standids.split(",");
                var newid="";
                for(var i=0 ; i < ids.length; i++){
                    if(ids[i]&&dataid!=ids[i]){
                        if(!newid){
                            newid+=ids[i];
                        }else{
                            newid+=","+ids[i];
                        }
                    }
                }
                $("#standIdStr").val(newid);
                $(this).remove();
            });
        });
    });
</script>
</body>
</html>
